import axios from "axios";
// import {  }
const instance = axios.create({
  baseURL: "http://geek.itheima.net/v1_0",
  timeout: 50000,
});

// 添加请求拦截
instance.interceptors.request.use((config) => {
  const token = localStorage.getItem("token");
  if (token) {
    config.headers = {
      Authorization: `Beaear ${token}`,
    };
  }

  return config;
});

// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    return response.data;
  },
  function (error) {
    if (error.response.status === 401) {
      localStorage.removeItem("token");
      // navigator('/login')
    }

    // 对响应错误做点什么
    return Promise.reject(error);
  },
);

export const http = instance;
